<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="param">
                <el-form-item>
                    <el-input v-model="param.keyword" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="searchKeyword">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="dic"
                  highlight-current-row v-loading="listLoading"
                  @selection-change="selsChange"
                  @cell-dblclick="celldblclick"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="sn" label="字典编号" width="120" >
            </el-table-column>
            <el-table-column prop="name" label="字典名称" width="120" >
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="param.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="操作" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="字典编号" prop="sn">
                    <el-input v-model="addForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="字典名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--字典明细-->
        <el-dialog :title="title" :visible.sync="detailTable" :close-on-click-modal="false">
            <!--列表-->
            <el-table :data="detail"
                      highlight-current-row v-loading="false"
                      style="width: 100%;" >
                <el-table-column type="index" width="60">
                </el-table-column>
                <el-table-column prop="name" label="明细名称" width="120" >
                </el-table-column>

                <el-table-column label="操作" width="150">
                    <template slot="header" slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleAdddetail">添加</el-button>
                    </template>
                    <template slot-scope="scope">
                        <el-button size="small" @click="handleEditdetail(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDeletedetail(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--工具条-->


                <el-pagination layout="prev, pager, next"
                               @current-change="handleCurrentChangedetail"
                               :current-page.sync="deparam.currentPage"
                               :page-size="deparam.pageSize"
                               :total="detotal"
                               style="float:right;">
                </el-pagination>

        </el-dialog>

        <!--新增界面-->
        <el-dialog title="操作" :visible.sync="adddetailvi" :close-on-click-modal="false">
            <el-form :model="addDetail" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="明细名称" prop="name">
                    <el-input v-model="addDetail.name" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmitde" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    export default {
        name: "dictionary",
        data(){
            return {

                param:{
                    currentPage:1,
                    pageSize:5,
                    keyword:''
                },
                dic:{},
                //列表加载框
                listLoading:false,
                sels:[],
                total:0,
                addFormVisible:false,
                addForm:{
                    sn:'',
                    name:''
                },
                addFormRules:{
                    sn: [
                        { required: true, message: '请输入字典编号', trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '请输入字典名称', trigger: 'blur' }
                    ]
                },
                addLoading:false,
                title:"",
                detailTable:false,
                detail:{},
                deparam:{
                    currentPage:1,
                    pageSize:3,
                    keyword:'',
                    types_id:0
                },
                detotal:0,

                adddetailvi:false,
                addDetail:{
                    name:'',
                    types_id:0
                }
            }
        },
        methods:{
            //双击单元格
            celldblclick(row, column, cell, event){
                if(column.property == "name"){
                    this.title="字典明细("+row.name+")",
                        this.deparam.currentPage = 1;
                    this.deparam.types_id = row.id;
                    this.addDetail.types_id = row.id;
                    this.loaddetail();
                  this.detailTable=true;
                }
            },
            //高级查询
            searchKeyword(){
                this.param.currentPage =1;
                this.loadDic();
            },
            // 新增弹框
            handleAdd(){
                this.addForm={
                    sn:'',
                        name:''
                };
                this.addFormVisible = true;
            },
            // 勾选触发函数
            selsChange(val){
                this.sels = val;
            },
            // 编辑触发函数
            handleEdit(index,row){
                this.addForm=Object.assign({},row);
                this.addFormVisible = true;
            },
            // 删除触发函数
            handleDel(index,row){
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("/dictype/" + row.id).then(res => {
                        this.loadDic();
                        if (res.data.success) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message('已取消删除');
                })
            },
            // 批量删除
            batchRemove(){
                // 拿到勾选的id数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                   this.$http.patch("/dictype",ids).then(res=>{
                       this.loadDic();
                       if (res.data.success) {
                           this.$message({
                               message: '删除成功',
                               type: 'success'
                           });
                       }else{
                           this.$message({
                               message: '网络异常',
                               type: 'warning'
                           });
                       }
                   })
                }).catch(()=>{
                    this.$message('已取消删除');
                })
            },
            //翻页触发函数
            handleCurrentChange(val){
                this.param.currentPage = val;
                this.loadDic();
            },
            //提交按钮
            addSubmit(){
                this.$http.put("/dictype",this.addForm).then(res=>{
                    this.addFormVisible=false;
                    this.loadDic();
                    if (res.data.success) {
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else{
                        this.$message({
                            message: '网络异常',
                            type: 'warning'
                        });
                    }
                })
            },
            loadDic(){
                this.$http.post("/dictype",this.param).then(res=>{
                    let {data,total} = res.data;
                    this.dic = data;
                    this.total=total;
                })
            },

            handleAdddetail(){
                this.addDetail={
                    name:'',
                    types_id:0
                };
                this.addDetail.types_id =this.deparam.types_id;
                this.adddetailvi = true;
            },
            handleEditdetail(index,row){
                this.addDetail=Object.assign({},row);
                // console.log(this.addDetail);
                this.adddetailvi = true;
            },
            addSubmitde(){
                this.$http.put("/dicdetail",this.addDetail).then(res=>{
                    this.adddetailvi=false;
                    this.loaddetail();
                    if (res.data.success) {
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else{
                        this.$message({
                            message: '网络异常',
                            type: 'warning'
                        });
                    }
                })
            },
            handleDeletedetail(index,row){
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("/dicdetail/" + row.id).then(res => {
                        this.loaddetail();
                        if (res.data.success) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message('已取消删除');
                })
            },
            handleCurrentChangedetail(val){
                this.deparam.currentPage = val;
                this.loaddetail();
            },
            loaddetail(){
                this.$http.post("/dicdetail",this.deparam).then(res=>{
                    // console.log(res);
                    this.detail= res.data.data;
                    this.detotal = res.data.total;
                })
            }
        },
        mounted(){
            this.loadDic();
        }
    }
</script>

<style scoped>

</style>